package com.kunya.product.service.impl.active;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Lists;
import com.kunya.common.common.ServerResponse;
import com.kunya.common.util.PropertiesUtil;
import com.kunya.product.dao.product.ProductMapper;
import com.kunya.product.po.Product;
import com.kunya.product.service.active.IHotProductService;
import com.kunya.product.vo.ProductListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author fuzhili
 * @Description:
 * @date 2018/7/16 16:25
 */
public class HotProductServiceImpl implements IHotProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public ServerResponse<PageInfo> findAllHotProduct(int pageNum, int pageSize,String tablePrefix) {
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("查询热门商品，缺失参数");
        }
        List<Product> list=productMapper.findAllHotProduct(tablePrefix);
        if(list==null){
            return ServerResponse.createByErrorMessage("查询热门商品，失败");
        }
        List<ProductListVo>  productVoList = Lists.newArrayList();
        for (Product product : list) {
            ProductListVo productListVo = assembleProductListVo(product);
            productVoList.add(productListVo);
        }
        PageInfo pageInfo=new PageInfo(list);
        return ServerResponse.createBySuccess("查询热门商品，成功",pageInfo);
    }
    //将product对象转化为list使用的vo对象
    private ProductListVo assembleProductListVo(Product product) {
        ProductListVo productListVo = new ProductListVo();
        BeanUtils.copyProperties(product,productListVo);
//        productListVo.setCategoryId(product.getCategoryId());
        //图片主机地址从配置文件中读取，不存数据库，为了迁移灵活
        productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
        return productListVo;
    }
}
